package com.bang.study.day01.stack;


import com.bang.study.day01.list.Array;

/**
 * @Auther: Bang
 * @Date: 2019/9/8 22:04
 * @Description: 基于数组实现的栈
 */
public class ArrayStack<E> implements Stack<E> {

    private Array<E> array;

    public ArrayStack() {
        array = new Array<>();
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public void push(E e) {
        array.addLast(e);
    }

    @Override
    public E pop() {
        return array.removeLast();
    }

    @Override
    public E peek() {
        return array.getLast();
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < array.getSize(); i++) {
            sb.append(array.get(i));
            if (i!=array.getSize()-1){
                sb.append(",");
            }
        }
        sb.append("]");
        return "ArrayStack{" +
                "array=" + sb.toString() +
                " top" +
                '}';
    }
}
